var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

//配置swagger
const swaggerInstall = require('./utils/swagger');
//导入cors中间件
const cors = require('cors');

var indexRouter = require('./routes/index');



let loginRouter = require('./routes/login.js');//数据库测试 引入login路由
let productRouter = require('./routes/product.js');// 引入product路由


var app = express();

let menusRouter= require('./routes/menus.js');// 引入menus路由

let categoryRouter =require('./routes/category.js')// 引入category路由

let customerRouter = require('./routes/custome.js');// 引入customer路由


let product_categoryRouter = require('./routes/product_category.js')// 引入product_category路由

let ordersRouter=require('./routes/orders11.js')// 引入orders路由
let privilegeRouter = require('./routes/privilege.js');

//调用swaggerInstall方法
swaggerInstall(app);

// 处理跨域
app.use(cors({
  origin: 'http://localhost:8080', 
  methods: ['GET', 'POST'], 
  allowedHeaders: ['Content-Type', 'Authorization']
}));


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/api', product_categoryRouter);
app.use('/api', customerRouter);
app.use('/', indexRouter);

app.use('/api',ordersRouter);

app.use('/api', loginRouter);//数据库测试

app.use('/api', productRouter);
app.use('/api', privilegeRouter);

app.use('/api',categoryRouter);


app.use('/api',menusRouter)

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

